package cn.bdqn.asecu.controller;

import cn.bdqn.asecu.service.AttributeManagementService;
import cn.bdqn.pojo.AttrList;
import cn.bdqn.pojo.Attributes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class AttributeManagementController {
    @Autowired
    AttributeManagementService attributeManagementService;

    @RequestMapping("attribute-management")
    public String html(Model model){
        List<Attributes> attributesList = attributeManagementService.findAll();
        model.addAttribute("attributesList",attributesList);
        return "attribute-management";
    }

    @RequestMapping("showAttrList")
    @ResponseBody
    public List<AttrList> showAttrList(Integer id){
        List<AttrList> attrLists = attributeManagementService.showAttrList(id);
        return attrLists;
    }

    @RequestMapping("attr-add")
    public String attr_add(){
        return "attr-add";
    }

    @RequestMapping("addAttr")
    @ResponseBody
    public String addAttr(Attributes attributes){
        attributes.setPurview("否");
        attributes.setAttrId(0);
        boolean b = attributeManagementService.addAttr(attributes);
        return b?"200":"error";
    }

    @RequestMapping("attr-edit")
    public String attr_edit(Model model,Integer id){
        Attributes attributes = attributeManagementService.findOne(id);
        model.addAttribute("attributes",attributes);
        return "attr-edit";
    }

    @RequestMapping("editAttr")
    @ResponseBody
    public String editAttr(Attributes attributes){
        boolean b = attributeManagementService.updateAttr(attributes);
        return b?"200":"error";
    }

    @RequestMapping("delAttr")
    @ResponseBody
    public String delAttr(Integer[] ids){
        attributeManagementService.delAttr(ids);
        return "200";
    }

    @RequestMapping("listattr-add")
    public String attrlist_add(Integer id,Model model){
        model.addAttribute("id",id);
        return "listattr-add";
    }

    @RequestMapping("addAttrList")
    @ResponseBody
    public String addAttrList(AttrList attrList){
        boolean b = attributeManagementService.addAttrList(attrList);
        return b?"200":"error";
    }

    @RequestMapping("listattr-edit")
    public String listattr_edit(Model model,Integer id){
        AttrList attrList = attributeManagementService.findOneAttrList(id);
        model.addAttribute("attrList",attrList);
        return "listattr-edit";
    }

    @RequestMapping("editAttrlist")
    @ResponseBody
    public String editAttrlist(AttrList attrList){
        boolean b = attributeManagementService.updateAttrList(attrList);
        return b?"200":"error";
    }
//
    @RequestMapping("delListattr")
    @ResponseBody
    public String delListattr(Integer[] ids){
        attributeManagementService.delListattr(ids);
        return "200";
    }
}
